package com.ohc.leetcode.链表;

import java.util.ArrayList;
import java.util.Stack;

/**
 * 2022年5月18日14:23:56
 * <p>
 * 9999999991+9
 */
public class 两数相加2 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l1.val == 0 & l2.val == 0) {
            return new ListNode(0, null);
        }
        Long sum1 = 0L;
        Long sum2 = 0L;
        Stack<Integer> stack1 = new Stack<>();
        Stack<Integer> stack2 = new Stack<>();
//         5 6 4
        int num1 = 0;
        int num2 = 0;
        long n1 = 1;
        long n2 = 1;
        while (l1 != null) {
            stack1.push(l1.val);
            if (num1 != 0) {
                n1 *= 10;
            }
            num1++;
            l1 = l1.next;
        }
        while (l2 != null) {
            stack2.push(l2.val);
            if (num2 != 0) {
                n2 *= 10;
            }
            num2++;
            l2 = l2.next;
        }

        while (!stack1.isEmpty()) {
            sum1 = sum1 + (n1 * stack1.pop());
            n1 /= 10;
        }
        while (!stack2.isEmpty()) {
            sum2 = sum2 + (n2 * stack2.pop());
            System.out.println("sum2 = " + sum2);
            n2 /= 10;
        }
        System.out.println("sum1 = " + sum1);
        System.out.println("sum2 = " + sum2);

        Long sum = sum1 + sum2;
        System.out.println("sum = " + sum);
        Long n = sum;
        long m = 0;
        ListNode head = new ListNode(0, null);  // 虚拟头结点
        ListNode first = head;
        ArrayList<Long> list = new ArrayList<>();
        while (n != 0) {
            m = n % 10;
            n = n / 10;
            list.add(m);
        }
        for (int i = 0; i < list.size(); i++) {
            first.next = new ListNode(Integer.parseInt(list.get(i) + ""), null);
            first = first.next;
        }
//        head = head.next;

//        return head;
        return head.next;
    }

    //         2 4 3  5 6 4
    public static void main(String[] args) {
        ListNode NodeL03 = new ListNode(2, null);
        ListNode NodeL02 = new ListNode(4, NodeL03);
        ListNode NodeL01 = new ListNode(3, NodeL02);

//        ListNode NodeR04 = new ListNode(9, null);


        ListNode NodeR03 = new ListNode(5, null);
        ListNode NodeR02 = new ListNode(6, NodeR03);
        ListNode NodeR01 = new ListNode(4, NodeR02);

        ListNode listNode = addTwoNumbers1(NodeL01, NodeR01);
        while (listNode != null) {
            System.out.println("listNode.val = " + listNode.val);
            listNode = listNode.next;
        }
//        int a = 9999999991;
    }



// 3 4 2    4 6 5
    public static ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
            ListNode head = null, tail = null;
    int carry = 0;
        while (l1 != null || l2 != null) {
        int n1 = l1 != null ? l1.val : 0;
        int n2 = l2 != null ? l2.val : 0;
        int sum = n1 + n2 + carry;
        if (head == null) {
            head = tail = new ListNode(sum % 10);
        } else {
            tail.next = new ListNode(sum % 10);
            tail = tail.next;
        }
        carry = sum / 10;
        if (l1 != null) {
            l1 = l1.next;
        }
        if (l2 != null) {
            l2 = l2.next;
        }
    }
        if (carry > 0) {
        tail.next = new ListNode(carry);
    }
        return head;
}


    }








    //         2 4 3  5 6 4


